package com.cashmanager.server.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.cashmanager.shared.vo.Cashier;
import com.cashmanager.shared.vo.CashierCompany;
import com.cashmanager.shared.vo.ValueObject;

public class CashierDaoImpl implements ICashierDao{	
	private static final String GET_ALL_CASHIER = "select * from " + SCHEMA_NAME+".get_all_cashier";
	private static final String GET_ALL_CASHIER_COMPANY = "select * from " + SCHEMA_NAME + ".CASHIER_COMPANY";
	private final Logger logger = Logger.getLogger(CashierDaoImpl.class);
	@Override
	public ArrayList<ValueObject> getAllCashier() {
		ArrayList<ValueObject> cashierList = new ArrayList<ValueObject>();
		Connection conn = null;
		try {
			conn = JDBCUtil.getInstance().getConnection();
			Statement stmt = conn.createStatement();
			logger.debug("Begin executing query for get all cashier");
			ResultSet rs = stmt.executeQuery(GET_ALL_CASHIER);
			logger.debug("After executing query for get all cashier");
			
			while (rs != null && rs.next()){
				Cashier cashier = new Cashier();
		
				int cashierId = rs.getInt(1);
				String cashierCode = rs.getString(2);
				String cashierSeq  = rs.getString(3);
				String cashierTypeCode = rs.getString(4);
				String cashierTypeName = rs.getString(5);
				String cashierCompanyName = rs.getString(6);
				String cashierCompanyContact = rs.getString(7);
				String cashierBrandCode = rs.getString(8);
				String cashierBrandName = rs.getString(9);
				String cashierModelCode = rs.getString(10);
				String cashierModelName = rs.getString(11);
				String cashierModelDescription = rs.getString(12);
	
				cashier.setcashierId(cashierId);
				cashier.setCashierCode(cashierCode);
				cashier.setCashierSeq(cashierSeq);
				cashier.setCashierTypeCode(cashierTypeCode);
				cashier.setCashierTypeName(cashierTypeName);
				cashier.setCashierCompanyName(cashierCompanyName);
				cashier.setCashierCompanyContact(cashierCompanyContact);
				cashier.setCashierBrandCode(cashierBrandCode);
				cashier.setCashierBrandName(cashierBrandName);
				cashier.setCashierModelCode(cashierModelCode);
				cashier.setCashierModelName(cashierModelName);
				cashier.setCashierModelDescription(cashierModelDescription);
				
				cashierList.add(cashier);

			}

			logger.debug("Fetched " + cashierList.size() + " cash from db.");	
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closeConn(conn);
		}
		return cashierList;
	}
	@Override
	public ArrayList<ValueObject> getAllCashierCompany() {
		logger.debug("Beging getAllCashierCompany");
		ArrayList<ValueObject> cashierComList = new ArrayList<ValueObject>();
		Connection conn = null;
		try {
				conn = JDBCUtil.getInstance().getConnection();
				Statement stmt = conn.createStatement();
				logger.debug(GET_ALL_CASHIER_COMPANY);
				ResultSet rs = stmt.executeQuery(GET_ALL_CASHIER_COMPANY);
				while (rs != null && rs.next()){
					CashierCompany com = new CashierCompany();
					int companyId = rs.getInt(1);
					String companyName = rs.getString(2);
					String companyContact = rs.getString(3);
					
					com.setCompanyId(companyId);
					com.setCompanyName(companyName);
					com.setCompanyContact(companyContact);
					cashierComList.add(com);
				} 
				logger.debug("Fetched " + cashierComList.size() + " cash from db.");
			}catch (SQLException e){
				e.printStackTrace();
			}finally{
				JDBCUtil.closeConn(conn);
			}
		return cashierComList;

	}
	public void addCompany(CashierCompany company) {
		
	}
}
